package sc.digital.human;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Objects;

public class Setup extends JDialog {
    private JTextField appId, appKey, secretKey, serviceId, skillId, chatAppKey, chatSecretKey;
    private JComboBox<Chat.Ver> chatVer;
    private JComboBox<Chat.Bot> chatBot;

    public Setup(JFrame owner) {
        super(owner, "参数设置", true);
        super.add(new Box(BoxLayout.Y_AXIS) {{
            this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            this.add(new JPanel() {{
                this.setBorder(BorderFactory.createTitledBorder("智能对话"));
                this.add(new Box(BoxLayout.Y_AXIS) {{
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("应用ＩＤ："));
                        this.add(appId = new JTextField(32));
                    }});
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("应用ＡＫ："));
                        this.add(appKey = new JTextField(32));
                    }});
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("应用ＳＫ："));
                        this.add(secretKey = new JTextField(32));
                    }});
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("服务版本："));
                        this.add(chatVer = new JComboBox<>(Chat.Ver.values()));
                    }});
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("服务ＩＤ："));
                        this.add(serviceId = new JTextField(32));
                    }});
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("技能ＩＤ："));
                        this.add(skillId = new JTextField(32));
                    }});
                }});
            }});
            this.add(new JPanel() {{
                this.setBorder(BorderFactory.createTitledBorder("文心一言"));
                this.add(new Box(BoxLayout.Y_AXIS) {{
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("应用ＡＫ："));
                        this.add(chatAppKey = new JTextField(32));
                    }});
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("应用ＳＫ："));
                        this.add(chatSecretKey = new JTextField(32));
                    }});
                    this.add(new Box(BoxLayout.X_AXIS) {{
                        this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                        this.add(new JLabel("服务名称："));
                        this.add(chatBot = new JComboBox<>(Chat.Bot.values()));
                    }});
                }});
            }});
        }}, BorderLayout.CENTER);
        this.appId.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if ("0123456789".indexOf(e.getKeyChar()) < 0) {
                    e.consume();
                }
            }
        });
        this.serviceId.setToolTipText(Config.HELP);
        this.skillId.setToolTipText(Config.HELP);
        super.add(new JPanel() {{
            this.setLayout(new FlowLayout(FlowLayout.RIGHT));
            this.add(new JButton("帮助") {{
                this.setFocusable(false);
                this.setFocusPainted(false);
                this.addActionListener((e) -> JOptionPane.showMessageDialog(owner, new JTextPane() {{
                    this.setContentType("text/html");
                    this.setText(Config.HELP);
                    this.setEditable(false);
                }}, this.getText(), JOptionPane.PLAIN_MESSAGE));
            }});
            this.add(new JButton("保存") {{
                this.setFocusable(false);
                this.setFocusPainted(false);
                this.addActionListener((e) -> {
                    Config.APP_ID = Integer.parseInt(appId.getText());
                    Config.APP_KEY = appKey.getText();
                    Config.SECRET_KEY = secretKey.getText();
                    Config.CHAT_VERSION = ((Chat.Ver) Objects.requireNonNull(chatVer.getSelectedItem())).name;
                    Config.SERVICE_ID = serviceId.getText();
                    Config.SKILL_ID = skillId.getText() == null || skillId.getText().trim().isEmpty()
                            ? null : skillId.getText().split(",");
                    Config.CHAT_APP_KEY = chatAppKey.getText();
                    Config.CHAT_SECRET_KEY = chatSecretKey.getText();
                    Config.CHAT_BOT_NAME = ((Chat.Bot) Objects.requireNonNull(chatBot.getSelectedItem())).name;
                    Config.writeConfig();
                    Setup.this.setVisible(false);
                });
            }});
            this.add(new JButton("取消") {{
                this.setFocusable(false);
                this.setFocusPainted(false);
                this.addActionListener((e) -> Setup.this.setVisible(false));
            }});
        }}, BorderLayout.SOUTH);
        super.pack();
        super.setLocationRelativeTo(null);
        super.setResizable(false);
    }

    @Override
    public void setVisible(boolean b) {
        if (b) {
            this.appId.setText(String.valueOf(Config.APP_ID));
            this.appKey.setText(Config.APP_KEY);
            this.secretKey.setText(Config.SECRET_KEY);
            this.chatVer.setSelectedItem(Chat.Ver.toVer(Config.CHAT_VERSION));
            this.serviceId.setText(Config.SERVICE_ID);
            this.skillId.setText(Config.SKILL_ID == null || Config.SKILL_ID.length == 0
                    ? null : String.join(",", Config.SKILL_ID));
            this.chatAppKey.setText(Config.CHAT_APP_KEY);
            this.chatSecretKey.setText(Config.CHAT_SECRET_KEY);
            this.chatBot.setSelectedItem(Chat.Bot.toBot(Config.CHAT_BOT_NAME));
        }
        super.setVisible(b);
    }
}
